package com.xkdx.guangguang.fragment.home.homedetail;

import java.util.HashMap;

import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.adapter.HomeDetailAdapter;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.home.homedetail.HomeDetailModule.HomeDetailAllInfo;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;

public class HomeDetailFragment extends BaseFragment {

	View view;
	ListView listView;
	Button back;
	TextView home_detail;
	String infoID;
	ImageView im;
	HomeDetailAllInfo homeDetailAllInfo;
	HomeDetailAdapter adapter;
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	HomeDetailAction homeDetailAction;
	HomeDetailModule homeDetailModule;
	HomeDetailPresistence homeDetailPresistence;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		infoID = getArguments().getString("infoID");
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.focuse_brand_discount)
				.showImageForEmptyUri(R.drawable.focuse_brand_discount)
				.showImageOnFail(R.drawable.focuse_brand_discount)
				.resetViewBeforeLoading().cacheOnDisc()
				.imageScaleType(ImageScaleType.EXACTLY)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.home_detail, container, false);

		findView();
		setOnClick();
		showLoading();
		return view;
	}

	@Override
	public void findView() {
		listView = (ListView) view.findViewById(R.id.lv_home_detail);
		back = (Button) view.findViewById(R.id.home_detail_back);
		im = (ImageView) view.findViewById(R.id.home_detail_im);
		home_detail = (TextView) view.findViewById(R.id.home_detail_tv);
		homeDetailAction = new HomeDetailAction(infoID);
		homeDetailModule = new HomeDetailModule();

	}

	@Override
	public void setOnClick() {
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (IConstants.isHasPush && !IConstants.isHasSelfPush) {
					IConstants.isHasPush = false;
					getActivity().finish();
				} else if (IConstants.isHasSelfPush) {
//					IConstants.isHasSelfPush = false;
					getActivity().finish();
				} else {
					getFragmentManager().popBackStack();
				}

			}
		});

	}

	@Override
	public void showLoading() {
		dataLoadDialog.show();
		homeDetailPresistence = new HomeDetailPresistence(this);
		homeDetailPresistence.setActitons(homeDetailAction);
		homeDetailPresistence.setModule(homeDetailModule);
		homeDetailPresistence.execute();
		dataLoadDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				if (homeDetailPresistence != null) {
					homeDetailPresistence.cancel(true);
				}
				getFragmentManager().popBackStack();

			}
		});
		dataLoadDialog.show();

	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			homeDetailAllInfo = homeDetailModule.homedetailAllInfo;
			adapter = new HomeDetailAdapter(getActivity(),
					homeDetailAllInfo.getHomeListInfoDetail());
			String url = homeDetailAllInfo.getHomeDtail().getPictureList()
					.get(0);
			listView.setAdapter(adapter);
			IConstants.setListViewHeightBasedOnChildren(listView);
			url = IConstants.addSuffix(url, "_L1");
			imageLoader.displayImage(url, im, options);
			home_detail.setText(homeDetailAllInfo.getHomeDtail()
					.getInfoContent());

		} else {
			showErrorTip(map);
		}

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

}
